package cn.zcib.tms.web;

import cn.zcib.tms.dao.UserMapper;
import cn.zcib.tms.model.User;
import org.apache.ibatis.reflection.ArrayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.thymeleaf.util.ArrayUtils;
import sun.plugin.javascript.navig.Array;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

@Controller
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @RequestMapping("/tologin")
    public String tologin() {
        return "login";
    }

    @RequestMapping("/toregist")
    public String toregist() {
        return "regist";
    }

    @RequestMapping("/logout")
    public String logout(Model model, HttpSession session, HttpServletResponse response,HttpServletRequest request) {
        User user = new User();
        user = (User)session.getAttribute("user");
        if (user != null) {
            session.removeAttribute("user");
            return "redirect:/index";
        } else {
            return "redirect:/index";
        }
    }

    @RequestMapping({"/admin/","/admin/index","/admin/login"})
    public String adminToLogin() {
        return "/manager/login";
    }

    @RequestMapping("/login")
    public String login(String u_name, String u_password, Model model, HttpSession session, HttpServletResponse response,HttpServletRequest request) throws IOException {
        System.out.println(u_name + u_password);

        User user = userMapper.login(u_name, u_password);

        if (user==null){
            model.addAttribute("u_name",u_name);
            model.addAttribute("message","用户名或密码错误11");
            return "login";
        }else {
            session.setAttribute("user",user);
            System.out.println(user);
            userMapper.updateLoginTime(new Date());
            response.sendRedirect(request.getContextPath()+"/index");
            return null;
        }

    }

    @RequestMapping("/adminLogin")
    public String adminLogin(String u_name, String u_password,HttpSession session,HttpServletRequest request,HttpServletResponse response){
       User user =  userMapper.login(u_name,u_password);
       if (user != null){
           if (user.getU_role()==1){
               session.setAttribute("user",user);
               userMapper.updateLoginTime(new Date());
               return "redirect:/manager/travelResources?type=1";
           }else{
               request.setAttribute("message","权限不足！");
               return "manager/login";
           }
       }else{
           request.setAttribute("message","用户名或密码错误！");
           return "/manager/login";
       }


    }


    @RequestMapping("/register")
    public String register(User user, Model model, HttpSession session,HttpServletResponse response,HttpServletRequest request) throws IOException {
        user.setU_role(0);
        user.setU_create_time(new Date());
        user.setU_login_time(new Date());
        userMapper.addUser(user);
        model.addAttribute("msg", "<script>alert('注册成功!');window.location.href='/login'</script>");
        return "regist";
    }

    @ResponseBody
    @RequestMapping("/register/checkname")
    public String checkName(String name, Model model, HttpSession session) {
        User user = userMapper.findUserByName(name);
        if (user != null) {
            return "{\"flag\":\"false\"}";
        }
        return "{\"flag\":\"true\"}";
    }


}
